<?php
	$temp_stamp = mktime(0,0,0, $month,$current_day,$year); 
	$week_offset = date('w', $temp_stamp);
?>

<TABLE class="overheadwkdisplay" border=0 cellspacing=0>
	<TR>
	<?php 

		$temp_day = $current_day - $week_offset;
		$temp_month = $month;
		$temp_year = $year;
		
		if ($temp_day<0){
			//get days in month of prev month
			$temp_month--;
			if ($temp_month<0){
				$temp_month=12;
				$temp_year--;
			}
			$ndaysprev=cal_days_in_month(0,$temp_month,$temp_year);
			$temp_day=$temp_day+$ndaysprev;

		}
		$startDate=$temp_year.'-'.$temp_month.'-'.$temp_day;
        	$newMonth=$temp_month;
		$newYear=$temp_year;
		$newDay=$temp_day;
		if ($temp_day+7 > cal_days_in_month(0,$month,$year)){
			$newDay=($temp_day+7)-cal_days_in_month(0,$month,$year);
			$newMonth++;
			if ($newMonth%13==0){
				$newMonth=1;
				$newYear++;
			}
		}
		$endDate=$newYear.'-'.$newMonth.'-'.$newDay;
	        //$events=outputCalendarByDateRange($client, $startDate, $endDate);
		$dayMap=array();
		$rDayMap=array();
		$counterM=$temp_month;
		$counterY=$temp_year;
		$counter=$temp_day;
		for ($j=0;$j<7;$j++){
			$rDayMap[$counter]=$j;
			$counter++;
			if($counter > cal_days_in_month(0,$counterM,$counterY)){
				$counter=1;
				$counterM++;
				if ($counterM%13==0){
					$counterM=1;
					$counterY++;
				}
			}	
		}
        
		$hours=array();
		for($i=0;$i<7;$i++){
			$hours[$i]=array();
		}
	        foreach($events as $event){
                $eventWhenArray=$event->when;
                foreach($eventWhenArray as $eventWhen){
                        $eHour=substr($eventWhen,19,2);
 			$eDay=substr($eventWhen,16,2);
			$fHour = substr($eventWhen,55,2) - 1;
			$fDay = substr($eventWhen,52,2);
                        if (substr($eHour,0,1)=="0"){
                                $eHour=substr($eHour,1,1);
                        }
			if (substr($fHour,0,1)=="0") {
				$fHour=substr($fHour,1,1);
			}
			if (substr($eDay,0,1)=="0"){
				$eDay=substr($eDay,1,1);
			}
			if (substr($fDay,0,1)=="0"){
				$fDay=substr($fDay,1,1);
			}
			$var='T';
			if (strpos($eventWhen,$var,4)!== false){
				while ($eDay <= $fDay) {
					while ($eHour <= $fHour && $eHour < 25) {
						$hours[$rDayMap[$eDay]][$eHour][]=$event;	
						$eHour++;
					}
					$eDay++;
				}
			}
			else {
                       		$hours[$rDayMap[$eDay]][25][]=$event;
			}
			
			
                }
        }


		echo "<TD  height=40>  </TD>";
	
		for ($week_day_count = 0; $week_day_count < 7; $week_day_count++) {
			echo "<TD height=40 width='12%' align=center class='weekdisplay'>";
			
			// if $temp_day is zero, step back a month.
			if ($temp_day <= 0) {
				if ($temp_month == 1) {
					$temp_month = 12;
					$temp_year = $temp_year - 1;
				}
				else {
					$temp_month = $temp_month - 1;
				}
				$temp_day = cal_days_in_month(0, $temp_month, $temp_year) + $temp_day;
			}
			else if ($temp_day > cal_days_in_month(0, $temp_month, $temp_year)) {
				$temp_day = $temp_day - cal_days_in_month(0, $temp_month, $temp_year);
				if ($temp_month == 12) {
					$temp_month = 1;
					$temp_year = $temp_year + 1;
				}
				else {
					$temp_month = $temp_month + 1;
				}
			}
			
			echo "$temp_month-$temp_day";
			$temp_day++;
			
			 /*echo "$month-";
			echo $current_day-$week_offset;
			$week_offset--; */
			
			echo "<BR>";
			switch ($week_day_count) {
				case 0: echo "Sun"; break;
				case 1: echo "Mon"; break;
				case 2: echo "Tues"; break;
				case 3: echo "Wed"; break;
				case 4: echo "Thurs"; break;
				case 5: echo "Fri"; break;
				case 6: echo "Sat"; break;
			}
			echo "</TD>";
		}
	?>
	<TD width=10></TD>
	</TR>
</TABLE>
<DIV id="weekOverflow">
<TABLE class="weekdisplay" border=1 cellspacing=0>	
	<?php
	echo "<TR><TD  align=right class='hourdisplay' height=10>All Day Events:</TD>";

	 for ($counter=0;$counter<7;$counter++){
	                $numeventstoday=count($hours[$counter][25]);
			if ($numeventstoday==0){
			echo "<TD height=10 width='12%' class='eventdisplay' valign=top><DIV id='whiteFont'>";
			}
			else{
			echo "<TD height=10 width='12%' valign=top><DIV id='whiteFont'>";
			}
			if ($numeventstoday>0){
			echo "<font size='2'>";
			foreach($hours[$counter][25] as $allDayEvent){
				if (strlen($allDayEvent->title) >= 10) {
					$eventTitle = substr(($allDayEvent->title),0,10);
					echo "<li class='".$allDayEvent->calColor."' style='background-color:".$allDayEvent->calColor."; filter:alpha(opacity=83);-moz-opacity:.83;opacity:.83; margin:0px; cursor:default; height:20px; padding-top:5px'><a style='cursor:default' onclick='editEvent(".$json->encode($allDayEvent).")'>".$eventTitle."...</a></li>";
				}
				else {
				echo "<li class='".$allDayEvent->calColor."' style='background-color:".$allDayEvent->calColor."; filter:alpha(opacity=83);-moz-opacity:.83;opacity:.83; margin:0px; cursor:default; height:20px; padding-top:5px'><a style='cursor:default' onclick='editEvent(".$json->encode($allDayEvent).")'>".$allDayEvent->title."</a></li>";
				}
			//echo "<li> $allDayEvent->title </li>";
			}
			echo "</font>";
			}
			echo "</DIV></TD>";

}
 echo "</TR><TR>";
         

	for ($hour=0; $hour<=24; $hour++) {
		if ($hour == 0) {
			$displayHour = 12;
			$am = true;
		}
		else if ($hour < 12 || $hour == 24) {
			$displayHour=$hour;
			$am = true;
			if ($displayHour==24){
				$displayHour=$hour/2;
			}
		}
		else {
			$displayHour=$hour-12;
			if ($displayHour==0){
				$displayHour=12;
			}
			$am = false;
		}
		echo "<TR><TD  align=right class='hourdisplay'>$displayHour";
		if ($am)
			echo ":00 am&nbsp;";
		else
			echo ":00 pm&nbsp;";
		echo "</TD>";
		
		for ($count=0; $count<7; $count++) {
			$numeventstoday=count($hours[$count][$hour]);
			if ($numeventstoday!=0){
				echo "<TD height=30 width='12%'><DIV id='whiteFont'>";
			}
			else{
				echo "<TD height=30 width='12%' class='eventDisplay'><DIV id='whiteFont'>";
			}
			if(($numeventstoday)!=0){
				echo "<font size='2'>";
					foreach ($hours[$count][$hour] as $event){
						if (strlen($event->title) >= 10) {
							$eventTitle = substr(($event->title),0,10);
							echo "<li class='".$event->calColor."' style='background-color:".$event->calColor."; filter:alpha(opacity=83);-moz-opacity:.83;opacity:.83; margin:0px; cursor:default; height:20px; text-align:center; padding-top:5px'><a style='cursor:default' onclick='editEvent(".$json->encode($event).")'>".$eventTitle."...</a></li>";
						}
						else {
							echo "<li class='".$event->calColor."' style='background-color:".$event->calColor."; filter:alpha(opacity=83);-moz-opacity:.83;opacity:.83; margin:0px; cursor:default; height:20px; text-align:center; padding-top:5px'><a style='cursor:default' onclick='editEvent(".$json->encode($event).")'>".$event->title."</a></li>";
						}
						//echo $event->title;
					}
				echo "</font>";
				}
			echo "</DIV></TD>";
		}
		echo "</TR>";
		//<TD colspan=7 width=560 height=30> </TD></TR>";
	}
	?>
</TABLE>
</DIV>
